package protocol;

import work_space.Drink;
import work_space.DrinkType;
import work_space.Order;

public class CommandParser {
    public static Order parseOrder(String command, String clientName) {
        if (!command.startsWith("order")) {
            throw new RuntimeException("invalid command");
        }
        Order order = new Order();
        order.setClientName(clientName);
        String[] splitCommand = command.split(" ");
        try {
            if (command.contains("and")) {
                // order all types of good
                int count1 = Integer.parseInt(splitCommand[1]);
                int count2 = Integer.parseInt(splitCommand[4]);
                // parse the first arg
                if (splitCommand[2].equals("tea")) {
                    for (int i = 0; i < count1; i++) {
                        Drink drink = new Drink(DrinkType.TEA);
                        order.getDrinkList().add(drink);
                    }
                } else if (splitCommand[2].equals("coffee")) {
                    for (int i = 0; i < count1; i++) {
                        Drink drink = new Drink(DrinkType.COFFEE);
                        order.getDrinkList().add(drink);
                    }
                }
                // parse the second arg
                if (splitCommand[5].equals("tea")) {
                    for (int i = 0; i < count2; i++) {
                        Drink drink = new Drink(DrinkType.TEA);
                        order.getDrinkList().add(drink);
                    }
                } else if (splitCommand[5].equals("coffee")) {
                    for (int i = 0; i < count2; i++) {
                        Drink drink = new Drink(DrinkType.COFFEE);
                        order.getDrinkList().add(drink);
                    }
                }
            } else {
                // just order single type
                int count = Integer.parseInt(splitCommand[1]);
                if (splitCommand[2].equals("tea")) {
                    for (int i = 0; i < count; i++) {
                        Drink drink = new Drink(DrinkType.TEA);
                        order.getDrinkList().add(drink);
                    }
                } else if (splitCommand[2].equals("coffee")) {
                    for (int i = 0; i < count; i++) {
                        Drink drink = new Drink(DrinkType.COFFEE);
                        order.getDrinkList().add(drink);
                    }
                } else {
                    throw new RuntimeException("invalid command");
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("invalid command");
        }
        return order;
    }
}
